<template>
    <div class="login-container flex-center"> 
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
            <div class="title-container">
                <h3 class="title">登录</h3>
            </div>

            <el-form-item prop="username" class="ai-center">
                <!--  <span class="icon-wrap flex-center"> 
                    <i class="icon el-icon-user-solid"></i>
                </span>  -->
                <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="请输入您的用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    autocomplete="off"
                />
            </el-form-item>

            <el-form-item prop="password" class="ai-center">
                <!-- <span class="icon-wrap"> 
                        <i class="icon el-icon-user-solid"></i>
                    </span> -->
                <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="请输入您的密码"
                    name="password"
                    tabindex="2"
                    autocomplete="off"
                    @keyup.enter.native="handleLogin"
                />
            </el-form-item>

            <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px" @click.native.prevent="handleLogin"
                >登录</el-button
            >
        </el-form>
    </div>
</template>

<script>
import {validUsername} from '@/utils/validate';
// import {loginApi} from '@/api/user';
// console.log(` loginApi ================= `);
// console.log(loginApi);
//   import SocialSign from './components/SocialSignin'

export default {
    name: 'Login',
    components: {
        // SocialSign
    },
    data() {
        const validateUsername = (rule, value, callback) => {
            if (!validUsername(value)) {
                callback(new Error('请输入您的用户名'));
            } else {
                callback();
            }
        };
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('请输入您的密码'));
            } else {
                callback();
            }
        };
        return { 
            loginForm: {
                username: 'admin',
                password: 'admin123',
                // username: '', // 'admin',
                // password: '' // 'admin123',
                // rememberMe: false,
            },
            loginRules: {
                username: [{required: true, trigger: 'blur', validator: validateUsername}],
                password: [{required: true, trigger: 'blur', validator: validatePassword}],
            },
            passwordType: 'password',
            capsTooltip: false,
            loading: false,
            redirect: undefined,
            otherQuery: {},
        };
    },
    watch: { 
        $route: {
            handler: function (route) {
                const query = route.query;
                if (query) {
                    this.redirect = query.redirect;
                    this.otherQuery = this.getOtherQuery(query);
                }
            },
            immediate: true,
        }
    },
    created() {
        // window.addEventListener('storage', this.afterQRScan)
    },
    mounted() {
        if (this.loginForm.username === '') {
            this.$refs.username.focus();
        } else if (this.loginForm.password === '') {
            this.$refs.password.focus();
        }
    },
    destroyed() {
        // window.removeEventListener('storage', this.afterQRScan)
    },
    methods: {
        handleLogin() { 
            this.$refs.loginForm.validate((valid) => {
                if (!valid) {
                    return false;
                }
                this.$store.dispatch('user/login', this.loginForm)
                    .then(res => {
                        console.log(res)
                        // const loginHOST = "http://192.168.1.155:9200" 登录接口的code 是200，别的接口的code 是0，所以暂时两个 都判断一下
                        if(res.code == 0 || res.code == 200){
                            console.log(' ok ================================= ')
                            this.$router.push({path: '/', query: this.otherQuery});
                        }
                    })
                    .catch(err => {
                        console.log(err)
                    });

                /* loginApi(this.loginForm).then(res=>{
                            if(res.code==0){
                                // this.$router.push({path: this.redirect || '/', query: this.otherQuery});
                                this.$router.push({path: '/' });
                                console.log(' 登录 ing  ')
                            }
                        }).catch(err=>{
                            console.error(err)
                        }) */
            });
        },
        getOtherQuery(query) {
            return Object.keys(query).reduce((acc, cur) => {
                if (cur !== 'redirect') {
                    acc[cur] = query[cur];
                }
                return acc;
            }, {});
        },
        // afterQRScan() {
        //   if (e.key === 'x-admin-oauth-code') {
        //     const code = getQueryObject(e.newValue)
        //     const codeMap = {
        //       wechat: 'code',
        //       tencent: 'code'
        //     }
        //     const type = codeMap[this.auth_type]
        //     const codeName = code[type]
        //     if (codeName) {
        //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
        //         this.$router.push({ path: this.redirect || '/' })
        //       })
        //     } else {
        //       alert('第三方登录失败')
        //     }
        //   }
        // }
    },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #2f4050;
$light_gray: #fff;
$cursor: #fff;
$dark_gray: #889aa4;

.login-container {
    width: 100%;
    min-height: 100%;
    height: 100vh;
    background-color: $bg;
    overflow: hidden;

    .login-form {
        position: relative;
        width: 350px;
        max-width: 100%;
        overflow: hidden;
    }

    .icon-wrap {
        color: $dark_gray;
        width: 40px;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .thirdparty-button {
        position: absolute;
        right: 0;
        bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
        .thirdparty-button {
            display: none;
        }
    }
}

/* reset element-ui css */
.login-container {
    .el-input {
        height: 47px;
        display: flex;
        flex: 1;
        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            flex: 1;
            padding: 12px 10px;
            color: $light_gray;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;

        .el-form-item__content {
            width: 100%;
            display: flex;
            align-items: center;
        }
    }
}
</style>
